home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1997 / HAM Radio 1997.iso / vcls / ras_api / rascomp.pas < prev    next >
Pascal/Delphi Source File  |  1996-04-08  |  12KB  |  404 lines

  1. unit RASComp;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, RAS_API;
  8.  
  9. { ********************************************************************** }
  10. { ********************************************************************** }
  11.  
  12. const
  13.   MaxConnections = 4;
  14.  
  15. type
  16.   TConnectionList = class(TList)
  17.     function AddConnection(Connection: TRASConn): Word;
  18.     function RASConn(Index: Integer): HRASConn;
  19.     function EntryName(Index: Integer): String;
  20.     procedure Delete(Index: Integer);
  21.   end;
  22.  
  23.   TRAS = class(TComponent)
  24.   private
  25.     { Private declarations }
  26.     fEntryName,
  27.     fPhoneNumber,
  28.     fPhoneBookPath,
  29.     fCallbackNumber,
  30.     fUserName,
  31.     fPassword,
  32.     fDomain,
  33.     fDeviceType,
  34.     fDeviceName: String;
  35.     fOnCallback,
  36.     fOnConnect,
  37.     fOnDisconnect: TNotifyEvent;
  38.     fWindowHandle: HWND;
  39.     RASEvent: Word;
  40.  
  41.     procedure SetPhoneBookPath(Value: String);
  42.     procedure Connected;
  43.     procedure DisConnected;
  44.     procedure WaitingForCallBack;
  45.     procedure WndProc(var Msg: TMessage);
  46.     function IntDisConnect: LongInt; { Used internally to bypass fOnDisconnect }
  47.   protected
  48.     { Protected declarations }
  49.   public
  50.     { Public declarations }
  51.     PhoneBookEntries: TStringList;
  52.     Connections: TConnectionList;
  53.     LastError: LongInt;
  54.     RASConn: HRASConn;   { Connection handle}
  55.     ConnectState: Word;
  56.     constructor Create(AOwner: TComponent); override;
  57.     destructor Destroy; override;
  58.     function GetConnectStatus: LongInt;
  59.     function DisConnect: LongInt;
  60.     function GetErrorString(ErrorCode: LongInt): String;
  61.     function Connect: LongInt;
  62.     function CurrentStatus: String;
  63.     function GetConnections: LongInt;
  64.     function GetPhoneBookEntries: LongInt;
  65.   published
  66.     { Published declarations }
  67.     property EntryName: String read fEntryName write fEntryName;
  68.     property PhoneNumber: String read fPhoneNumber write fPhoneNumber;
  69.     property PhoneBookPath: String read fPhoneBookPath write SetPhoneBookPath;
  70.     property CallbackNumber: String read fCallbackNumber write fCallbackNumber;
  71.     property UserName: String read fUserName write fUserName;
  72.     property Password: String read fPassword write fPassword;
  73.     property Domain: String read fDomain write fDomain;
  74.     property DeviceType: String read fDeviceType write fDeviceType;
  75.     property DeviceName: String read fDeviceName write fDeviceName;
  76.     property OnConnect: TNotifyEvent read fOnconnect write fOnConnect;
  77.     property OnDisconnect: TNotifyEvent read fOnDisconnect write fOnDisconnect;
  78.     property OnCallBack: TNotifyEvent read fOnCallBack write fOnCallBack;
  79.   end;
  80.  
  81. procedure Register;
  82.  
  83. implementation
  84.  
  85. procedure Register;
  86. begin
  87.   RegisterComponents('RAS', [TRAS]);
  88. end;
  89.  
  90. { ********************************************************************* }
  91. {                           TConnectionList                             }
  92. { ********************************************************************* }
  93. function TConnectionList.AddConnection(Connection: TRASConn): Word;
  94. var
  95.   Conn: PRASConn;
  96. begin
  97.   Conn := New(PRASConn);
  98.   Conn^ := Connection;
  99.   Add(Conn);
  100. end;
  101.  
  102. function TConnectionList.RASConn(Index: Integer): HRASConn;
  103. begin
  104.   Result := PRASConn(Items[Index])^.RASConn;
  105. end;
  106.  
  107. function TConnectionList.EntryName(Index: Integer): String;
  108. begin
  109.   If PRASConn(Items[Index])^.szEntryName[0] <> #0 THEN
  110.      Result := StrPas(PRASConn(Items[Index])^.szEntryName)
  111.   ELSE
  112.      Result := '';
  113. end;
  114.  
  115. procedure TConnectionList.Delete(Index: Integer);
  116. begin
  117.   Dispose(PRASConn(Items[Index]));
  118.   Items[Index] := Nil;
  119.   inherited Delete(Index);
  120. end;
  121.  
  122. { ********************************************************************* }
  123. {                            TRASConnection                             }
  124. { ********************************************************************* }
  125. constructor TRAS.Create(AOwner: TComponent);
  126. begin
  127.   inherited Create(AOwner);
  128.   RASEvent := RegisterWindowMessage(RASDialEvent);
  129.   If RASEvent = 0 THEN
  130.      RASEvent := WM_RASDialEvent;
  131.   RASConn := 0;
  132.   ConnectState := 0;
  133.   fWindowHandle := 0;
  134.   PhoneBookEntries := TStringList.Create;
  135.   Connections := TConnectionList.Create;
  136. end;
  137.  
  138. destructor TRAS.Destroy;
  139. begin
  140.   IntDisconnect;
  141.   PhoneBookEntries.Free;
  142.   Connections.Free;
  143.   inherited Destroy;
  144. end;
  145.  
  146. function TRAS.Connect: LongInt;
  147. var
  148.   RASDialParams: TRASDialParams;{ points to calling parameters }
  149.   R: LongInt;
  150.   Blank: String;
  151. begin
  152.   If RASConn <> 0 THEN { Allow only one connection }
  153.      IntDisConnect;
  154.   If fWindowHandle = 0 THEN
  155.      fWindowHandle := AllocateHWnd(WndProc);
  156.   FillChar(RASDialParams, SizeOf(RASDialParams), #0);
  157.   With RASDialParams DO
  158.   Begin
  159.     dwSize := SizeOf(RASDialParams);
  160.     Blank := fEntryName + #0;
  161.     StrLCopy(szEntryName, @Blank[1], RAS_MaxEntryName);
  162.     Blank := fPhoneNumber + #0;
  163.     StrLCopy(szPhoneNumber, @Blank[1], RAS_MaxPhoneNumber);
  164.     Blank := fCallBackNumber + #0;
  165.     StrLCopy(szCallbackNumber, @Blank[1], RAS_MaxCallbackNumber);
  166.     Blank := fUserName + #0;
  167.     StrLCopy(szUserName, @Blank[1], UNLEN);
  168.     Blank := fPassWord + #0;
  169.     StrLCopy(szPassword, @Blank[1], PWLEN);
  170.     Blank := fDomain + #0;
  171.     StrLCopy(szDomain, @Blank[1], DNLEN);
  172.   End;
  173.   If fPhoneBookPath <> '' THEN
  174.   Begin
  175.     Blank := fPhoneBookPath + #0;
  176.     LastError := RasDial(Nil, @Blank[1], @RASDialParams, Nil, fWindowHandle, RASConn);
  177.   End
  178.   ELSE
  179.     LastError := RasDial(Nil, Nil, @RASDialParams, Nil, fWindowHandle, RASConn);
  180.   Result := LastError;
  181. end;
  182.  
  183. function TRAS.GetErrorString(ErrorCode: LongInt): String;
  184. var
  185.   szErrorString: Array[0..256] of Char;
  186. begin
  187.   Result := '';
  188.   If (RASConn = 0) THEN
  189.      Exit;
  190.   FillChar(szErrorString, SizeOf(szErrorString), #0);
  191.   RasGetErrorString(ErrorCode, szErrorString, 256);
  192.   If szErrorString[0] <> #0 THEN
  193.      Result := StrPas(szErrorString)
  194.   Else
  195.      Result := 'Status Unknown'; 
  196. end;
  197.  
  198. function TRAS.Disconnect: LongInt;
  199. begin
  200.   Result := 0;
  201.   If RASConn <> 0 THEN
  202.      Result := RASHangUp(RASConn);
  203.   RASConn := 0;
  204.   If fWindowHandle <> 0 THEN { Stop message flow }
  205.   Begin
  206.     DeallocateHWnd(fWindowHandle);
  207.     fWindowHandle := 0;
  208.   End;
  209.   LastError := Result;
  210.   Disconnected;
  211. end;
  212.  
  213. function TRAS.IntDisconnect: LongInt;
  214. begin
  215.   Result := 0;
  216.   If RASConn <> 0 THEN
  217.      Result := RASHangUp(RASConn);
  218.   RASConn := 0;
  219.   If fWindowHandle <> 0 THEN { Stop message flow }
  220.   Begin
  221.     DeallocateHWnd(fWindowHandle);
  222.     fWindowHandle := 0;
  223.   End;
  224.   LastError := Result;
  225. end;
  226.  
  227. function TRAS.GetConnectStatus: LongInt;
  228. var
  229.   RASConnStatus: TRASConnStatus;
  230. begin
  231.   If (RASConn = 0) THEN
  232.      Exit;
  233.   FillChar(RASConnStatus, SizeOf(RASConnStatus), #0);
  234.   RASConnStatus.dwSize := 60;
  235.   LastError := RasGetConnectStatus(RASConn, @RASConnStatus);
  236.   If LastError = 0 THEN
  237.   begin
  238.     fDeviceName := StrPas(RASConnStatus.szDeviceName);
  239.     fDeviceType := StrPas(RASConnStatus.szDeviceType);
  240.     ConnectState := RASConnStatus.rasconnstate;
  241.   end;
  242.   LastError := RASConnStatus.dwError;
  243.   Result := LastError;
  244. end;
  245.  
  246. function TRAS.GetConnections: LongInt;
  247. var
  248.   RASConnect: Array[1..MaxConnections] OF TRASConn;
  249.   I,
  250.   BufSize,
  251.   NumConnections: Word;
  252. begin
  253.   Connections.Clear;
  254.   RASConnect[1].dwSize := 30;
  255.   BufSize := SizeOf(RASConnect);
  256.   Result := RasEnumConnections(@RASConnect, BufSize, NumConnections);
  257.   LastError := Result;
  258.   If (Result = 0) OR (Result = ERROR_BUFFER_TOO_SMALL) THEN
  259.      For I := 1 TO NumConnections DO
  260.          If (I <= MaxConnections) THEN
  261.             Connections.AddConnection(RASConnect[I]);
  262. end;
  263.  
  264. function TRAS.GetPhoneBookEntries;
  265. var
  266.   RASEntryName: Array[1..20] Of TRASENTRYNAME;
  267.   I,
  268.   BufSize,
  269.   Entries: Word;
  270.   szPhoneBookPath: PChar;
  271. begin
  272.   PhoneBookEntries.Clear;
  273.   RASEntryName[1].dwSize := 26;
  274.   BufSize := SizeOf(RASEntryName);
  275.   If fPhoneBookPath <> '' THEN
  276.   Begin
  277.     GetMem(szPhoneBookPath, Length(fPhoneBookPath) + 1);
  278.     StrPCopy(szPhoneBookPath, fPhoneBookPath);
  279.     Result := RasEnumEntries(Nil, szPhonebookPath, @RASEntryName,
  280.                              BufSize, Entries);
  281.     FreeMem(szPhoneBookPath, Length(fPhoneBookPath) + 1);
  282.   End
  283.   ELSE
  284.     Result := RasEnumEntries(Nil, Nil, @RASEntryName, BufSize, Entries);
  285.   LastError := Result;
  286.   If (Result = 0) OR (Result = ERROR_BUFFER_TOO_SMALL) THEN
  287.      For I := 1 TO Entries DO
  288.          If ( I < 21) AND (RASEntryName[I].szEntryName[0] <> #0) THEN
  289.             PhoneBookEntries.Add(StrPas(RASEntryName[I].szEntryName));
  290. end;
  291.  
  292. procedure TRAS.WndProc(var Msg: TMessage);
  293. begin
  294.   If (Msg.Msg = RASEvent) AND (RASConn <> 0) THEN
  295.   Begin
  296.     If Msg.lParam <> 0 THEN
  297.        LastError := Msg.lParam
  298.     ELSE
  299.     Begin
  300.       ConnectState := Msg.wParam;
  301.       Case ConnectState OF
  302.            RASCS_DeviceConnected: Connected;
  303.            RASCS_WaitForCallBack: WaitingForCallBack;
  304.       End;
  305.     End;
  306.     CurrentStatus;
  307.   End
  308.   ELSE
  309.      DefWindowProc(fWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  310. end;
  311.  
  312. function TRAS.CurrentStatus: String;
  313. begin
  314.   Result := '';
  315.   If (RASConn = 0) THEN
  316.   Begin
  317.     Result := 'Not Active';
  318.     Exit;
  319.   End;
  320.   If RASConn <> 0 THEN
  321.   Begin
  322.     GetConnectStatus;
  323.     If LastError <> 0 THEN
  324.     Begin
  325.       If LastError >= 600 THEN
  326.          Result := GetErrorString(LastError)
  327.       ELSE
  328.       Case LastError OF
  329.         6: Result := 'Invalid Handle';
  330.         8: Result := 'Not enough memory';
  331.       End;
  332.     End
  333.     ELSE
  334.     Case ConnectState OF
  335.      RASCS_OpenPort:
  336.             Result := 'Opening Port';
  337.      RASCS_PortOpened:
  338.             Result := 'Port Opened';
  339.      RASCS_ConnectDevice:
  340.             Result := 'Using Device: ' + fDeviceName + ' - ' + fDeviceType;
  341.      RASCS_DeviceConnected:
  342.             Result := 'Device is connected';
  343.      RASCS_AllDevicesConnected:
  344.             Result := 'All Required Devices Connected';
  345.      RASCS_Authenticate:
  346.             Result := 'Validating User/Password/Domain';
  347.      RASCS_AuthNotify:
  348.             Result := 'Authentication Notification';
  349.      RASCS_AuthCallBack:
  350.             Result := 'Authentication Call Back';
  351.      RASCS_AuthProject:
  352.             Result := 'Project';
  353.      RASCS_AuthLinkSpeed:
  354.             Result := 'Calculating Link speed';
  355.      RASCS_AuthAck:
  356.             Result := 'Authentication acknowledged';
  357.      RASCS_ReAuthenticate:
  358.             Result := 'Reauthenticating';
  359.      RASCS_Authenticated:
  360.             Result := 'Login Authenticated';
  361.      RASCS_PrepareforCallBack:
  362.             Result := 'Preparing for Callback';
  363.      RASCS_WaitForModemReset:
  364.             Result := 'Waiting for Modem Reset';
  365.      RASCS_WaitForCallBack:
  366.             Result := 'Waiting for Callback';
  367.     End; { Case }
  368.   End
  369.   ELSE
  370.     Result := 'Not Connected';
  371. end;
  372.  
  373. procedure TRAS.SetPhoneBookPath(Value: String);
  374. begin
  375.   fPhoneBookPath := Value;
  376.   GetPhoneBookEntries;
  377. end;
  378.  
  379. procedure TRAS.Connected;
  380. begin
  381.   If (RASConn = 0) THEN
  382.      Exit;
  383.   If Assigned(fOnConnect) THEN
  384.      fOnConnect(Self);
  385. end;
  386.  
  387. procedure TRAS.DisConnected;
  388. begin
  389.   If (RASConn = 0) THEN
  390.      Exit;
  391.   If Assigned(fOnDisConnect) THEN
  392.      fOnDisConnect(Self);
  393. end;
  394.  
  395. procedure TRAS.WaitingForCallBack;
  396. begin
  397.   If (RASConn = 0) THEN
  398.      Exit;
  399.   If Assigned(fOnCallBack) THEN
  400.      fOnCallBack(Self);
  401. end;
  402.  
  403. end.
  404.